#include "../core/engine/engine.h"
#include "grid.h"

namespace sleek
{
    namespace node
    {
        namespace real
        {
            Grid::Grid(core::engine::engine *smgr, texture::pixel clr) : Node(smgr)
            {
                call = glGenLists(1);
                space = core::math::aabbox2df(core::math::vector2df(-150,-150), core::math::vector2df(150.5,150.5));
                color = clr;
                size = 10;

                renderGrid();
            }
            Grid::~Grid()
            {
                if(glIsList(call))
                    glDeleteLists(call, 1);
            }
            void Grid::setQuadSize(core::f32 i)
            {
                size = i;
                renderGrid();
            }
            void Grid::setSpace(core::math::aabbox2df i)
            {
                space = i;
                renderGrid();
            }
            void Grid::setColor(texture::pixel i)
            {
                color = i;
                renderGrid();
            }
            texture::pixel Grid::getColor()
            {
                return color;
            }
            core::math::aabbox2df Grid::getSpace()
            {
                return space;
            }
            core::f32 Grid::getQuadSize()
            {
                return size;
            }
            void Grid::render()
            {
                glCallList(call);
            }
            void Grid::renderGrid()
            {
                glNewList(call , GL_COMPILE);
                float x, z;
                Node::render();
                for(x = space.getUpperLeft().width; x<space.getLowerRight().width; x += size)
                    smgr->getDrawManager()->drawLine(core::math::vector3df(x,0,space.getUpperLeft().height), core::math::vector3df(x,0,space.getLowerRight().height), color);
                for(z = space.getUpperLeft().height; z<space.getLowerRight().height; z += size)
                    smgr->getDrawManager()->drawLine(core::math::vector3df(space.getUpperLeft().width,0,z), core::math::vector3df(space.getLowerRight().width,0,z), color);
                glEndList();
            }
        }
    }
}
